package cn.jinbyte.core.handler.crypt;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;

@Slf4j
@SuppressWarnings("unused")
public class Base64CryptHandler implements CryptHandler {
    @Override
    public <T> String encode(T value) {
        if (value == null) {
            return null;
        }
        // 编码逻辑（根据类型序列化为字符串）
        String rawValue = serializeToString(value, value.getClass());
        return Base64.encodeBase64String(rawValue.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public <T> T decode(String encryptedValue, Class<T> tClass) {
        if (StringUtils.isEmpty(encryptedValue)) {
            return null;
        }
        String rawValue = new String(Base64.decodeBase64(encryptedValue.getBytes(StandardCharsets.UTF_8)));
        return parseFromString(rawValue, tClass);
    }

}
